-------------------------------------------------------------
--ECE 251 Switch - Packager of 4bits to 48bits block - by Peter Gebhard--
-------------------------------------------------------------

LIBRARY ieee;
USE ieee.std_logic_1164.all;

ENTITY packager4to48 IS
PORT (clk      : IN   STD_LOGIC;
      d        : IN   STD_LOGIC_VECTOR(3 DOWNTO 0);
      d_valid  : IN   STD_LOGIC;
      d_buff	: BUFFER STD_LOGIC_VECTOR(47 DOWNTO 0);
	   q        : OUT STD_LOGIC_VECTOR(47 DOWNTO 0);
	   q_valid  : OUT STD_LOGIC
);
END packager4to48;

ARCHITECTURE Behavior OF packager4to48 IS
	--SIGNAL d_buff : STD_LOGIC_VECTOR(47 DOWNTO 0);
	SIGNAL 	i: integer;
BEGIN
	PROCESS (clk)
	BEGIN
		IF (clk'EVENT and clk = '1') THEN
			IF i < 0 THEN
				i <= 0;
				d_buff <= "000000000000000000000000000000000000000000000000";
			END IF;
			IF (d_valid = '1') THEN 
				IF i = 0 THEN 
					d_buff(47 DOWNTO 44) <= d;
					i <= i + 1; 
	            ELSIF i = 1 THEN
					d_buff(43 DOWNTO 40) <= d;
					i <= i + 1; 
				-- Switch validity of output to zero after two cycles if we 
	            -- recognize that we are getting new data from the port
	            ELSIF i = 2 THEN
	 				q_valid <= '0';
					d_buff(39 DOWNTO 36) <= d;
					i <= i + 1;
	            ELSIF i = 3 THEN
					d_buff(35 DOWNTO 32) <= d;
					i <= i + 1;
	            ELSIF i = 4 THEN
					d_buff(31 DOWNTO 28) <= d;
					i <= i + 1;
	            ELSIF i = 5 THEN
					d_buff(27 DOWNTO 24) <= d;
					i <= i + 1;
	            ELSIF i = 6 THEN
					d_buff(23 DOWNTO 20) <= d;
					i <= i + 1;
	            ELSIF i = 7 THEN
					d_buff(19 DOWNTO 16) <= d;
					i <= i + 1;
	            ELSIF i = 8 THEN
					d_buff(15 DOWNTO 12) <= d;
					i <= i + 1;
	            ELSIF i = 9 THEN
					d_buff(11 DOWNTO 8) <= d;
					i <= i + 1;
	            ELSIF i = 10 THEN
					d_buff(7 DOWNTO 4) <= d;
					i <= i + 1;
	            ELSIF i = 11 THEN
					d_buff(3 DOWNTO 0) <= d;
					i <= i + 1;
		     	-- Done with 48bit repackaging of incoming data
	         	ELSIF i = 12 THEN
					i <= -100; 
	            	q_valid <= '1';
	            	q <= d_buff;
	         	END IF;      
			END IF;
	      
		END IF;
	END PROCESS;
END Behavior;
